import Vue from 'vue'
import Router from 'vue-router'
import {logout} from '@/api/api'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const menuCommon = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  }, {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }, {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/index'),
      meta: {title: '首页', icon: 'el-icon-s-home'}
    }]
  }, {
    path: '/authMgr',
    component: Layout,
    meta: {title: '权限管理', icon: 'el-icon-setting'},
    children: [{
      path: '/auth',
      name: 'Auth',
      component: () => import('@/views/auth/auth'),
      meta: {title: '权限管理', icon: 'el-icon-s-home'}
    }, {
      path: '/role',
      name: 'Role',
      component: () => import('@/views/auth/role'),
      meta: {title: '角色管理', icon: 'el-icon-s-home'}
    }, {
      path: '/sys',
      name: 'Sys',
      component: () => import('@/views/auth/sys'),
      meta: {title: '系统管理', icon: 'el-icon-setting'}
    }, {
      path: '/org',
      name: 'Org',
      component: () => import('@/views/auth/org'),
      meta: {title: '单位管理', icon: 'el-icon-setting'}
    }]
  }, {
    path: '/accountMgr',
    component: Layout,
    meta: {title: '账号管理', icon: 'el-icon-setting'},
    children: [{
      path: '/account',
      name: 'Account',
      component: () => import('@/views/auth/account'),
      meta: {title: '账号管理', icon: 'el-icon-s-home'}
    }]
  }]

export const menuStats = {
  path: '/stats',
  component: Layout,
  name: 'Stats',
  meta: {title: '数据统计', icon: 'el-icon-s-data'},
  children: []
}

export const menu404 = {path: '/*', redirect: '/404', hidden: true}

// export const menuSetting = {
//   path: '/setting',
//   component: Layout,
//   name: 'Setting',
//   meta: {title: '系统设置',icon: 'el-icon-setting'},
//   children: [{
//     path: '/changePwd',
//     name: 'ChangePwd',
//     component: () => import('@/views/member/changePwd'),
//     meta: {title: '修改密码', icon: 'el-icon-edit-outline'}
//   }, {
//     path: '/bindGoogle',
//     name: 'BindGoogle',
//     component: () => import('@/views/setting/googleValidator'),
//     meta: {title: '绑定谷歌验证器', icon: 'el-icon-lock'}
//   }, {
//     path: '/logout',
//     name: 'Logout',
//     component: () => import('@/views/login'),
//     beforeEnter (to, from, next) {
//       logout().then(res => {
//         next()
//       })
//     },
//     meta: {title: '退出登录', icon: 'el-icon-switch-button'}
//   }]
// }

const getRoutesByRole = function () {
  let user = {type: 1};//JSON.parse(localStorage.getItem("accountInfo"));
  // return customRoutes['common'].concat(customRoutes['waiter']).concat(customRoutes['setting']).concat(customRoutes['404'])
  let rs = menuCommon
  // {1: "平台", 2 : "商户", 3: "店长", 4: "店员", 6: "财务", 7: "客服", 99: "管理员"};
  rs.push(menuStats)
  rs.push(menu404)
  return rs
}

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({x: 0, y: 0}),
  // routes: constantRoutes
  routes: getRoutesByRole()
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
